package com.csx.governance.traffic.adapter.outbound.webclient;

import org.springframework.http.HttpHeaders;
import org.springframework.web.reactive.function.client.ClientRequest;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
import org.springframework.web.reactive.function.client.ExchangeFunction;
import reactor.core.publisher.Mono;

/**
 * @author cuisongxu
 * @date 2023/4/12 21:31
 */
public class TrafficExchangeFilterFunction implements ExchangeFilterFunction {

    @Override
    public Mono<ClientResponse> filter(ClientRequest request, ExchangeFunction next) {

        System.out.println(request);
        System.out.println(next);
        return Mono.just(request)
                .flatMap(req -> Mono.just(ClientRequest.from(req)
                        .header(HttpHeaders.AUTHORIZATION, "xxx")
                        .build()))
                .flatMap(next::exchange);
    }

    @Override
    public ExchangeFilterFunction andThen(ExchangeFilterFunction afterFilter) {
        System.out.println("andThen");
        return ExchangeFilterFunction.super.andThen(afterFilter);
    }

    @Override
    public ExchangeFunction apply(ExchangeFunction exchange) {
        System.out.println("xxx");
        System.out.println(exchange);
        return ExchangeFilterFunction.super.apply(exchange);
    }
}
